package logic

import (
	"context"
	"goodsrpc/goods"

	"api/internal/svc"
	"api/internal/types"
	"github.com/golangblogs/jwt"
	"github.com/zeromicro/go-zero/core/logx"
)

type LogonLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLogonLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogonLogic {
	return &LogonLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LogonLogic) Logon(req *types.LogonRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	logon, err := l.svcCtx.Goods.Logon(l.ctx, &goods.LogonRequest{
		Mobile: req.Mobile,
		Code:   req.Code,
	})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msy:  err.Error(),
			Data: nil,
		}, nil
	}
	j := jwt.NewJWT()
	newjwt := jwt.CustomClaims{ID: uint(logon.Id)}
	token, err := j.CreateToken(newjwt)
	if err != nil {
		return nil, err
	}
	return &types.Response{
		Code: 200,
		Msy:  "登录成功成功",
		Data: token,
	}, nil
}
